#include <linMasterTask.h>
#include <ColorMixingTask.h>
#if LIN_MASTER_EN == 1

TaskState_t linmTaskState = TASK_STATE_INIT;
LIN_Device_Frame_t activelinmFrame;

uint8_t commandCount = 0;

typedef struct{
  uint8_t red;
  uint8_t green;
  uint8_t blue;
}RGB_t;

RGB_t rgbmTable[7] = {
  {255,0,0},
  {0,255,0},
  {0,0,255},
  {255,255,0},
  {0,255,255},
  {255,0,255},
  {255,255,255},
};
uint8_t index = 0;


LIN_Device_Frame_t linm_cmd_table[] = {
    [0] = {
        .frame_id = 0x06,
        .msg_type = LIN_MSG_TYPE_TX,
        .checksum = LIN_CHECKSUM_CLASSIC,
        .length   = LIN_BUFF_SIZE,
    },
};


void linmSendTimerExpired(SoftTimer_t *timer)
{
//    Color_CMDM_Frame_t *frame;
//    frame = (Color_CMDM_Frame_t *)linm_cmd_table[0].data;
//    if (commandCount ==0){
//        SoftTimer_Stop(timer);
//        timer->interval = 10;
//        SoftTimer_Start(timer);
////        CLM_SetRGBL(ROOM_TEMPERATURE, rgbmTable[index].red, rgbmTable[index].green, rgbmTable[index].blue, 1000, 2000);
//    }else{
//        linm_cmd_table[0].frame_id = commandCount;
//        frame->command = COLOR_CMDM_RGB;
//        frame->ColorRGB.red   = rgbmTable[index].red;
//        frame->ColorRGB.green = rgbmTable[index].green;
//        frame->ColorRGB.blue  = rgbmTable[index].blue;
//        frame->ColorRGB.level = 1000;
//        frame->ColorRGB.transitionTime = 20;
//        LINM_SendFrame(&linm_cmd_table[0]);
//    }
//    commandCount++;
//    if (commandCount > 5){
//        commandCount = 0;
//        index++;
//        if (index >= 7)index = 0;
//        SoftTimer_Stop(timer);
//        timer->interval = 2000;
//        SoftTimer_Start(timer);
//    }
}


SoftTimer_t linmSendTimer = {
    .mode     = TIMER_PERIODIC_MODE,
    .interval = 10,
    .handler  = linmSendTimerExpired
};


void LIN_Master_ISR(LIN_Device_Frame_t *frame)
{
    activelinmFrame = *frame;
    TM_PostTask(TASK_ID_LINM);
}

void hanldeLinSlaveCommands(LIN_Device_Frame_t *frame)
{
    // Handle received command
}

void LINM_TaskHandler(void)
{
    switch(linmTaskState){
    case TASK_STATE_ACTIVE:
//      hanldeLinSlaveCommands(&activelinmFrame);
      break;
    case TASK_STATE_INIT:
      LINM_Init();
      LINM_RegisterLINM_IRQ(LIN_Master_ISR);
      
      SoftTimer_Start(&linmSendTimer);
      linmTaskState = TASK_STATE_ACTIVE;
      break;
    }
}

#endif
